package com.neuedu.corejava.ch12.homework;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyFile {

	public static void cyfile() {

		File f1 = new File("d:\\io\\opysrc.doc");
		f1.getParentFile().mkdirs();

		FileOutputStream fOutputStream = null;
		try {
			if (f1.exists()) {
				f1.delete();
				System.out.println("有重复文件，删除成功！");
			} else {
				f1.createNewFile();
				System.out.println("创建文件成功");
			}

			fOutputStream = new FileOutputStream(f1);
			for (int i = 0; i < 100; i++) {
				String str = "Hello File" + i + "\r\n";
				byte[] by = str.getBytes();
				fOutputStream.write(by);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		File dest = new File("d:\\io\\copysrc.doc");
		try (BufferedReader br = new BufferedReader(new FileReader(f1));
			 BufferedWriter bw = new BufferedWriter(new FileWriter(dest));) {

			String content = null;
			while ((content = br.readLine()) != null) {
				bw.write(content);
				bw.newLine();
			}
			System.out.println("拷贝成功！");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		cyfile();

	}

}
